﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using System.Configuration;
using DevExpress.LookAndFeel;
using BellAssistant.Common.CommonHelper;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraTab;
using BellAssistant.SalsManager;
using DevExpress.XtraTab.ViewInfo;
using BellAssistant.DataManager;

namespace BellAssistant
{
    public partial class FrmMain : FrmMainBase
    {

        public static RibbonControl MainRibbonControl { get; set; }
        private bool IsClosedForm = false;
        private bool IsChangeUser = false;
        public FrmMain()
        {
            InitializeComponent();

            #region 设置背景图相关
            this.panel_Main.Dock = DockStyle.Fill;

            this.picEdit_Main.Parent = this.panel_Main;
            this.picEdit_Main.BorderStyle = BorderStyles.NoBorder;
            this.picEdit_Main.Dock = DockStyle.Fill;

            this.tab_Main.Parent = this.picEdit_Main;
            this.tab_Main.BorderStyle = BorderStyles.NoBorder;
            this.tab_Main.Dock = DockStyle.Fill;
            this.tab_Main.Visible = false;

            this.picEdit_Main.Properties.SizeMode = PictureSizeMode.Stretch;  //图片布局方式
            this.picEdit_Main.Properties.BorderStyle = BorderStyles.NoBorder; //禁止显示边框
            this.picEdit_Main.Properties.AllowDisposeImage = true;            //允许释放资源
            this.picEdit_Main.Properties.ShowMenu = false; //隐藏右键菜单
            this.picEdit_Main.Properties.ReadOnly = true;  //只读
            this.picEdit_Main.Properties.AllowScrollViaMouseDrag = false;     //禁止拖拽图片
            this.picEdit_Main.Properties.AllowZoomOnMouseWheel = DefaultBoolean.False;   //禁用鼠标滚轮缩放图片
            this.picEdit_Main.Properties.AllowScrollOnMouseWheel = DefaultBoolean.False; //禁止鼠标上下滚动
            #endregion

            
        }

        #region ShowForm
        private void ShowForm(Type type)
        {
            Form frm = null;
            try
            {
                frm = type.Assembly.CreateInstance(type.ToString()) as Form;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog.Error(ex.Message, ex);
                MessageBox.Show("窗体实例化失败！请及时将信息反馈给我公司研发部！");
            }
            if (frm != null)
            {
                if (tab_Main.TabPages.Count > 0)
                {
                    foreach (XtraTabPage item in tab_Main.TabPages)
                    {
                        if (item.Text == frm.Text)
                        {
                            tab_Main.SelectedTabPage = item;
                            return;
                        }
                    }
                }
                else
                {
                    tab_Main.Visible = true;
                    picEdit_Main.Image = null;
                }

                frm.TopLevel = false;
                frm.Dock = DockStyle.Fill;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.WindowState = FormWindowState.Maximized;

                XtraTabPage tabPage = new XtraTabPage();
                tabPage.Text = frm.Text;
                tabPage.Controls.Add(frm);
                tab_Main.TabPages.Add(tabPage);
                tab_Main.SelectedTabPage = tabPage;

                frm.Show();
            }
        }
        #endregion


       

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                MainRibbonControl = this.ribbonControl1;

                //设置系统样式
                object objSkinName = ConfigurationManager.AppSettings["SkinName"];
                if (objSkinName != null && !string.IsNullOrEmpty(objSkinName.ToString()))
                    UserLookAndFeel.Default.SetSkinStyle(objSkinName.ToString());
                else
                    InitSkins();

                //设置状态栏信息
                bsi_LoginUserName.Caption = "当前用户：" + "贝儿";
                //bsi_SystemVersion.Caption = "系统版本：" + CreateDataSourceService.IDataSourceService.GetSystemVersion();
                bsi_SystemTime.Caption = "系统时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

             
            }
            catch (Exception ex)
            {
                //LogHelper.WriteLog.Error(ex.Message);
                MessageBox.Show(ex.Message, Msg.Warning);
            }
        }

        #region 初始化系统样式
        private void InitSkins()
        {
            this.ribbonControl1.RibbonStyle = RibbonControlStyle.Office2010; //设置Ribbon风格
            this.ribbonControl1.ShowExpandCollapseButton = DefaultBoolean.False; //隐藏收缩按钮
            this.ribbonControl1.ShowToolbarCustomizeItem = false; //显示左上角的倒三角

            SkinHelper.InitSkinGallery(skinGalleryBarItem, true);
            UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
        }
        #endregion

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!IsChangeUser)
            {
                if (!IsClosedForm)
                {
                    if (MessageBox.Show("确定退出系统？", Msg.Infor, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        IsClosedForm = true;
                        Application.Exit();
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
            IsChangeUser = false;
        }

        private void tab_Main_CloseButtonClick(object sender, EventArgs e)
        {
            ClosePageButtonEventArgs arg = (ClosePageButtonEventArgs)e;
            XtraTabPage page = (XtraTabPage)arg.Page;
            foreach (var item in page.Controls)
            {
                Form frm = item as Form;
                if (frm != null)
                {
                    frm.Dispose();
                    frm.Close();
                }
            }
            page.Dispose();
            if (tab_Main.TabPages.Count == 0)
            {
                tab_Main.Visible = false;
            }
        }

        #region 销售管理
        private void bbi_GoodsManager_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(GoodsManager));
        }

        #endregion


        #region 数据管理
        private void bbi_CommissionManager_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(CommissionManager));
        }

        private void bbi_GoodsCategory_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(GoodsCategoryManage));
        }

        //品牌管理
        private void bbi_BrandManage_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(BrandManage));
        }

        //促销类别管理
        private void bbi_PromotionManage_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(PromotionManage));
        }

        //快递类别管理
        private void bbi_ExpressTypeManage_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(typeof(ExpressTypeManage));
        }


        #endregion


    }
}